package com.geek.novel.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.geek.novel.R;
import com.geek.novel.activity.book.ViewerActivity;
import com.geek.novel.entity.BookIndex;
import com.geek.novel.entity.BookRead;
import com.geek.novel.utils.FormatUtil;
import com.geek.novel.utils.SPUtil;
import org.jetbrains.annotations.NotNull;
import skin.support.content.res.SkinCompatResources;

import java.util.ArrayList;

/**
 * 阅读页面章节目录的适配器
 */
public class BookSectionIndexAdapter extends BaseRecyclerViewAdapter<BookSectionIndexAdapter.ViewHolder, BookIndex> {

    /**文本颜色*/
    private final int textColorActive;

    public BookSectionIndexAdapter(ViewerActivity context, ArrayList<BookIndex> list){
        super(context, list);

        //当前章节显示的字体颜色
        this.textColorActive = context.getResources().getColor(R.color.book_dialog_section_index_active);

        //初始化排序
        super.initSortDesc();
    }

    //定义内部类ViewHolder，并继承RecyclerView.ViewHolder传入View参数通常是Recycler View子项的最外层布局
    public static class ViewHolder extends BaseRecyclerViewHolder {
        TextView tvSectionIndexName;
        TextView tvSectionIndexCached;

        @SuppressWarnings("unchecked")
        public ViewHolder(View itemView, BaseRecyclerViewAdapter adapter) {
            super(itemView, adapter);
            tvSectionIndexName = itemView.findViewById(R.id.tv_section_index_name);
            tvSectionIndexCached = itemView.findViewById(R.id.tv_section_index_cached);
        }
    }


    /**获取排序字段保存到文件中的key*/
    @Override
    public String getSortDescSpKey() {
        return SPUtil.book_index_sort_desc;
    }


    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_book_section_index, parent,false);
        return new ViewHolder(view, this);
    }

    @Override
    public void onBindViewHolder(@NotNull ViewHolder holder, int position) {
        ViewerActivity context = (ViewerActivity)getContext();
        //处理倒序
        int index = super.getIndex(position);
        //获取章节索引信息
        BookIndex bookIndex = super.getList().get(index);
        //章节名称
        holder.tvSectionIndexName.setText(bookIndex.getIndexName());
        //是当前章节，标红
        BookRead book = context.getBook();
        if(book != null){
            int curReadIndex = book.getCurReadIndex();
            if(curReadIndex == index){
                holder.tvSectionIndexName.setTextColor(textColorActive);
            }else{
                //获取颜色，主要用于适配主题
                int textColor = SkinCompatResources.getColor(context, R.color.book_dialog_text_color);
                holder.tvSectionIndexName.setTextColor(textColor);
            }
        }

        //是否已经缓存
        boolean crawler = FormatUtil.isNullToBoolean(bookIndex.getCrawler(), false);
        holder.tvSectionIndexCached.setVisibility(crawler?View.VISIBLE :View.INVISIBLE);

        //调用父类的方法，主要用于绑定事件
        super.onBindViewHolder(holder, position);
    }

}
